package cn.midas.system.controller;

import cn.midas.basic.utils.AjaxResult;
import cn.midas.basic.utils.PageList;
import cn.midas.system.domain.Dict;
import cn.midas.system.query.DictQuery;
import cn.midas.system.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/dict")
public class DictController {
    @Autowired
    private IDictService dictService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Dict loadById(@ApiParam(value="t_dict表主键",required = true) @PathVariable("id") Long id) {
        return dictService.loadById(id);
    }

    @GetMapping
    public List<Dict> loadAll() {
        return dictService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {

        try {
            dictService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    @PutMapping
    public AjaxResult uptOrAdd(@RequestBody Dict dict) {
        try {
            if (dict.getId()==null) {
                dictService.save(dict);
            }else {
                dictService.update(dict);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }


    }
    @PostMapping
    public PageList<Dict> queryData(@RequestBody DictQuery dictQuery) {
        return dictService.queryData(dictQuery);
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids) {

        try {
            dictService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }



}
